package com.zy.controller;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

//http://localhost:8080/ai2
@RestController
public class AiController2 {
    private final ChatClient chatClient;
    private final ChatMemory chatMemory;

    public AiController2(ChatClient.Builder chatClientBuilder, ChatMemory chatMemory) {
        this.chatClient = chatClientBuilder.defaultSystem("""
               你是一个用户管理客服，专门管理用户的增删改查功能，
               你要要求客户出示姓名,只要出示姓名就可以删除，然后调用相关的函数执行操作。
            """)
                .defaultAdvisors(
                        new PromptChatMemoryAdvisor(chatMemory)
                )
                .defaultFunctions("deleteUserFunction")
                .build();
        this.chatMemory = chatMemory;
    }

    @PostMapping (value = "/ai2" ,produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public String generation(@RequestBody String message) {
        // 创建一个 Flux 来处理可能的多个响应
        String responseStream = this.chatClient.prompt()
                .user(message)
                .system("你是一个用户管理客服，专门管理用户的增删改查功能，" +
                        "只有当用户向你提出增删改查功能性的要求时，" +
                        "你要要求客户出示姓名然后你执行函数，不要确认")
                // 设置记忆信息最大数
                .advisors(advisorSpec -> advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY,100))
                .call()
                .content();
        return responseStream;
    }
}
